package com.prs.crm.action.hr;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;


import com.prs.crm.domain.hr.TrainItem;
import com.prs.crm.service.hr.TrainItemService;
import com.prs.crm.service.sys.LogService;

public class TrainItemAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private TrainItem trainItem;
	private TrainItemService trainItemService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listTrainItem")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				TrainItem.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTrainItemService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editTrainItem")
	public String edit() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveTrainItem", results = {
			@Result(name = "success", type = "redirect", location = "listTrainItem"),
			@Result(name = "error", type = "chain", location = "editTrainItem") })
	public String save() {
		if (trainItem.getName() == null
				|| "".equals(trainItem.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;

		}
		if (this.getLogService().getSearchName(TrainItem.class,
				trainItem.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.setPrompt("提交培训项目成功");
		this.getTrainItemService().save(trainItem);
		return SUCCESS;
	}

	@Action(value = "removeTrainItem", results = {
			@Result(name = "success", type = "redirect", location = "listTrainItem"),
			@Result(name = "error", type = "chain", location = "listTrainItem") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的培训项目");
			return ERROR;
		}
		for (Integer id : ids) {
			TrainItem trainItem = this.getTrainItemService().get(id);
			if (trainItem.getTrains().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getTrainItemService().batchRemove(ids);
		return SUCCESS;
	}

	public TrainItem getTrainItem() {
		return trainItem;
	}

	public void setTrainItem(TrainItem trainItem) {
		this.trainItem = trainItem;
	}

	public TrainItemService getTrainItemService() {
		return trainItemService;
	}

	public void setTrainItemService(TrainItemService trainItemService) {
		this.trainItemService = trainItemService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
